package com.cmccgz.udacs_landlord_webapp.main.user.userinfo;

import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.cmccgz.baselibrary.base.BaseFragment;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;

/**
 * @author INX
 * 修改密码fragment
 */
public class UserPwdChangedFragment extends BaseFragment implements View.OnClickListener ,UserInfoContract.ChangePwdView{
    private UserInfoContract.ChangePwdPresenter presenter;
    private Button btn_ok;
    private EditText et_old_pwd,et_new_pwd,et_new_pwd_re;
    private CheckBox cb_show_hide;

    public static UserPwdChangedFragment instance() {
        return new UserPwdChangedFragment();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_pwd_change;
    }

    @Override
    protected void initView() {
        btn_ok = ((Button) findViewById(R.id.btn_ok));
        et_old_pwd = ((EditText) findViewById(R.id.et_userName));
        et_new_pwd = ((EditText) findViewById(R.id.et_new_pwd));
        et_new_pwd_re = ((EditText) findViewById(R.id.et_new_pwd_re));
        cb_show_hide = ((CheckBox) findViewById(R.id.cb_show_hide));
        cb_show_hide.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    et_old_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    et_new_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    et_new_pwd_re.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    et_old_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    et_new_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    et_new_pwd_re.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        btn_ok.setOnClickListener(this);
        ViewHelper viewHelper = ViewHelper.instance(this);
        if (viewHelper != null) {
            viewHelper.setAllVisable(false);
            viewHelper.setBackTextVisable(true);
            viewHelper.setTitle(R.string.title_pwd_change);
        }
        new UserPwdChangedPresenter(this);
        if (isSaveInstance&&getSavedInstanceState()!=null) {
            Bundle savedInstanceState = getSavedInstanceState();
            String newPWD = savedInstanceState.getString("newPWD", "");
            String newPWD_RE = savedInstanceState.getString("newPWD_RE", "");
            String oldPWD = savedInstanceState.getString("oldPWD", "");
            et_new_pwd.setText(newPWD);
            et_new_pwd_re.setText(newPWD_RE);
            et_old_pwd.setText(oldPWD);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                mContext.onBackPressed();
                break;
            case R.id.btn_ok:
                showLoadingDialog(getString(R.string.loading_change_pwd));
                presenter.changePwd(et_old_pwd.getText().toString(),
                        et_new_pwd.getText().toString(),
                        et_new_pwd_re.getText().toString());
                break;
        }
    }

    /**
     * 修改密码成功
     */
    @Override
    public void changePwdSuccess() {
        dismissLoadingDialog();
        showToast(R.string.tips_pwd_change_success);
        mContext.onBackPressed();
    }

    /**
     * 修改密码成失败
     */
    @Override
    public void changePwdError(String msg) {
        dismissLoadingDialog();
        showToast(msg);
    }

    @Override
    public void setPresenter(UserInfoContract.ChangePwdPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString("newPWD",et_new_pwd.getText().toString());
        outState.putString("newPWD_RE",et_new_pwd_re.getText().toString());
        outState.putString("oldPWD",et_old_pwd.getText().toString());
        super.onSaveInstanceState(outState);
    }
}
